package com.bianmaba.controllers.base.support;

import com.bianmaba.commons.bean.result.OperationResult;
import com.bianmaba.spring.mvc.annotation.RequestResponseBodyConfig;
import com.bianmaba.spring.mvc.config.annotation.EntityParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Api(description = "支持增、删、改、查的API接口")
public abstract class CrudControllerSwaggerSupport<T, ID extends Serializable> extends ReadControllerSwaggerSupport<T, ID> {

    @RequestResponseBodyConfig
    @RequestMapping(
            value = {"/save"},
            headers = {"Content-Type=application/json"}, method = RequestMethod.POST
    )
    public OperationResult<T> saveFromJson(@RequestBody T entity) {
        return this.save(entity);
    }

    @ApiOperation("数据保存接口，支持POST请求，接收FormData数据( application/x-www-form-urlencoded )或JSON数据(Content-type=application/json)")
    @RequestResponseBodyConfig
    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    public OperationResult<T> saveFromFormdata(@EntityParam T entity) {
        return this.save(entity);
    }

    protected OperationResult<T> save(T entity) {
        T result = this.getService().save(entity);
        return new OperationResult(true, "数据保存成功", result);
    }

    @RequestResponseBodyConfig
    @RequestMapping(
            value = {"/saves"},
            headers = {"Content-Type=application/json"}, method = RequestMethod.POST
    )
    public OperationResult<List<T>> savesFromJson(@RequestBody List<T> entires) {
        return this.save(entires);
    }

    @ApiOperation("数据集保存接口，支持POST请求，接收FormData数据( application/x-www-form-urlencoded )或JSON数据(Content-type=application/json)")
    @RequestResponseBodyConfig
    @RequestMapping(value = {"/saves"}, method = RequestMethod.POST)
    public OperationResult<List<T>> savesFromFormdata(@EntityParam List<T> entires) {
        return this.save(entires);
    }

    protected OperationResult<List<T>> save(List<T> entires) {
        List<T> result = this.getService().save(entires);
        return new OperationResult(true, "数据保存成功", result);
    }

    @ApiOperation("根据数据ID集合删除多条数据，支持POST请求，接收JSON数据Content-type=application/json")
    @RequestResponseBodyConfig
    @RequestMapping(
            value = {"/deletes"},
            headers = {"Content-Type=application/json"}, method = {RequestMethod.POST}
    )
    public OperationResult<ID[]> deletes(@RequestBody ArrayList<ID> ids) {
        this.getService().delete(ids);
        return new OperationResult(true, "数据删除成功", ids);
    }

    @ApiOperation("根据数据ID删除数据，支持POST请求，接收FormData数据(Content-type=application/x-www-form-urlencoded)和QueryString")
    @RequestResponseBodyConfig
    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    public OperationResult<ID> delete(ID id) {
        this.getService().delete(id);
        return new OperationResult(true, "数据删除成功", id);
    }

}
